package com.itheima.kafka_demo.test;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.logging.log4j.core.appender.mom.kafka.KafkaProducerFactory;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;

import java.util.Properties;

public class ProducerQuickStart {
    public static void main(String[] args) {
        //1.kafka相关配置
        Properties properties = new Properties();
            //1.1 kafka地址
            properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
            //1.2 发送失败，失败的重试次数
            properties.put(ProducerConfig.RETRIES_CONFIG,5);
            //1.3 消息key的序列化器
            properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
            //1.4 消息value的序列化
            properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //生产对象
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);
        //封装发送信息

        ProducerRecord<String,String> record = new ProducerRecord<String, String>("itheima-topic","100001","hello kafka");

        //发送信息
        producer.send(record);
        //关闭生产者通道，必须关闭，否则消息会发送失败
        producer.close();

    }
}
